﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentNightsight : Component, IUpdateable
    {
        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public FCModLoaderShengcheng m_modloader = new FCModLoaderShengcheng();

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public float m_NightseeDuration;

        public Random m_random = new Random();

        public bool IsActive => m_NightseeDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartNightsightBuff(float time)
        {
            m_NightseeDuration = time;
            float blockLight = 5f;
            UpdateBrightness(blockLight);
        }

        public void StopBuff()
        {
            if (m_componentPlayer != null)
            {
                m_NightseeDuration = 0f;
                m_componentPlayer.ComponentGui.DisplaySmallMessage("夜视结束！", Color.Purple, blinking: true, playNotificationSound: false);
                float brightness = SettingsManager.Brightness;
                UpdateBrightness(brightness);
            }
        }

        public void UpdateBrightness(float blockLight)
        {
            float x = MathUtils.Lerp(0f, 0.1f, blockLight);
            for (int i = 0; i < 16; i++)
            {
                LightingManager.LightIntensityByLightValue[i] = MathUtils.Saturate(MathUtils.Lerp(x, 1f, MathUtils.Pow((float)i / 15f, 1.25f)));
            }

            for (int j = 0; j < 6; j++)
            {
                float num = LightingManager.CalculateLighting(CellFace.FaceToVector3(j));
                for (int k = 0; k < 16; k++)
                {
                    LightingManager.LightIntensityByLightValueAndFace[k + j * 16] = LightingManager.LightIntensityByLightValue[k] * num;
                }
            }

            m_subsystemTerrain.TerrainUpdater.DowngradeAllChunksState(TerrainChunkState.InvalidPropagatedLight, forceGeometryRegeneration: true);
        }

        public void Update(float dt)
        {
            if (IsActive)
            {
                if (m_NightseeDuration > 0f)
                {
                    m_NightseeDuration -= dt;
                }

                if (m_NightseeDuration <= 0f)
                {
                    StopBuff();
                }
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            m_NightseeDuration = valuesDictionary.GetValue("NightDuration", 0f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("NightDuration", m_NightseeDuration);
        }
    }
}